﻿/*
 * Created by SharpDevelop.
 * User: tdptcj
 * Date: 17/01/2012
 * Time: 9:07
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;

using MetaVideoEditorEvolved.UI.Entity;

namespace MetaVideoEditorEvolved.Converter
{
	/// <summary>
	/// Description of MediaFilterConverter.
	/// </summary>
	public class MediaFilterConverter : IMultiValueConverter
	{
		private MetaVideoEditorEvolved.Entity.MediaFilter filter;
		
		public MediaFilterConverter()
		{
		}
		
		public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (values == null || values.Length!=2 || values[0] == null || values[1] == null) {
				return null;
			}
			
			this.filter = (MetaVideoEditorEvolved.Entity.MediaFilter)values[1];
			
			var view = new ListCollectionView((System.Collections.IList)values[0]);
			view.Filter+= this.FilterMedia;
			
			return view;
		}
		
		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotSupportedException("Not implemented");

		}
		
		private bool FilterMedia(object obj)
		{
			var media = obj as MediaItemBaseEntity;
			var returnMedia = true;
			
			if((this.filter.Filter & MetaVideoEditorEvolved.Entity.FilterType.MediaType) == MetaVideoEditorEvolved.Entity.FilterType.MediaType)
			{
				if (this.filter.MediaType== MetaVideoEditorEvolved.Entity.MediaType.Movie) {
					returnMedia = (media is MovieMediaItem) || (media is FolderMediaItem);
				}
				
				if (this.filter.MediaType== MetaVideoEditorEvolved.Entity.MediaType.Serie) {
					returnMedia = (media is SerieMediaItem) || (media is FolderMediaItem);
				}
			}
			
			if(returnMedia)
			{
				if ((this.filter.Filter & MetaVideoEditorEvolved.Entity.FilterType.DataState) == MetaVideoEditorEvolved.Entity.FilterType.DataState) {
					int percentvalue;
					int percentMinValue;
					
					switch (this.filter.DataStateLevel) {
						case 0:
							percentMinValue = 0;
							percentvalue = 20;
							break;
							
						case 1:
							percentMinValue = 21;
							percentvalue = 69;
							break;
							
						default:
							percentMinValue = 70;
							percentvalue = 100;
							break;
					}
					
					var video = media as VideoMediaElement;
					
					if (video!=null) {
						var serie = video as SerieMediaItem;
						
						if (serie!=null) {
							returnMedia &= this.SearchInSerie(serie, percentMinValue, percentvalue);
						}
						else
						{
							var season = video as SeasonMediaItem;
							
							if (season!=null) {
								returnMedia &= this.SearchInSeason(season, percentMinValue, percentvalue);
							}
							else
							{
								returnMedia &= video.DataState<=percentvalue && video.DataState>=percentMinValue;
							}
						}
					}
				}
			}
			
			if (returnMedia) {
				if((this.filter.Filter & MetaVideoEditorEvolved.Entity.FilterType.TitleSearch) == MetaVideoEditorEvolved.Entity.FilterType.TitleSearch)
				{
					var video = media as VideoMediaElement;
					
					if(video==null)
					{
						returnMedia&= media.Name.ToLower().Contains(this.filter.Title.ToLower());
					}
					else
					{
						var originalTitle = string.IsNullOrEmpty(video.OriginalTitle) ? string.Empty : video.OriginalTitle.ToLower();
						var videoTitle = string.IsNullOrEmpty(video.Title) ? string.Empty : video.Title.ToLower();
						
						returnMedia&= originalTitle.Contains(this.filter.Title.ToLower()) || videoTitle.ToLower().Contains(this.filter.Title.ToLower()) || video.Name.ToLower().Contains(this.filter.Title.ToLower());
					}
				}
			}
			
			return returnMedia;
		}
		
		private bool SearchInSerie(SerieMediaItem serie, int percentMinValue, int percentvalue)
		{
			foreach (var season in serie.Seasons)
			{
				if (this.SearchInSeason(season, percentMinValue, percentvalue))
				{
					return true;
				}
			}
			
			return serie.DataState<=percentvalue && serie.DataState>=percentMinValue;
		}
		
		private bool SearchInSeason(SeasonMediaItem season, int percentMinValue, int percentvalue)
		{
			foreach (var episode in season.Episodes) 
			{
				if (episode.DataState<=percentvalue && episode.DataState>=percentMinValue) 
				{
					return true;
				}
			}
			
			return false;
		}
	}
}
